/*********************************************************************
 *** 名称：GameAudio.cpp
 *** 描述：GameAudio类中定义方法的具体实现。
 ********************************************************************/
#include "GameAudio.h"

GameAudio::GameAudio(){

	if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 4096) != 0){
		std::cout<<"Error when try to open audio device: "<<Mix_GetError()<<std::endl;
		exit(1);
	}

	Mix_Volume(-1, 128);
	Mix_VolumeMusic(80);

	this->music = NULL;
	this->sound = NULL;
}

GameAudio::~GameAudio(){
	Mix_HaltChannel(-1);
	Mix_HaltMusic();
	Mix_FreeChunk(sound);
	Mix_FreeMusic(music);
	Mix_CloseAudio();
}

void GameAudio::playMusic(const char *musicFile){
	Mix_HaltMusic();
	Mix_FreeMusic(music);

	music = Mix_LoadMUS(musicFile);
	if(music == NULL){
		std::cout<<"Error when load music file: "<<Mix_GetError()<<std::endl;
		exit(1);
	}

	Mix_PlayMusic(music, -1);
}

void GameAudio::playSound(const char *soundFile){
	Mix_HaltChannel(1);
	Mix_FreeChunk(sound);

	sound = Mix_LoadWAV(soundFile);
	if(sound == NULL){
		std::cout<<"Error when load sound file: "<<Mix_GetError()<<std::endl;
		exit(1);
	}

	Mix_PlayChannel(1, sound, 0);
}

void GameAudio::closeSound(){
    Mix_Pause(-1);
    Mix_PauseMusic();
}

void GameAudio::resumeSound(){
    Mix_Resume(1);
    Mix_ResumeMusic();
}
